﻿using System.Linq;
using INHollandair.Data;

namespace INHollandair.Core.Repositories
{
    public class UserRepository
    {
        private readonly PresentatieDBDataContext _context = new PresentatieDBDataContext();


        /// <summary>
        /// Repo's - Services voeren hier hun queries op uit
        /// </summary>
        public IQueryable<Models.User> UserRepo
        {
            get
            {
                return _context.Users.Select(x => new Models.User
                {
                    Id = x.ID,
                    Name = x.Name,
                    Hash = x.Hash,
                    Salt = x.Salt,
                    Email = x.Email,
                    Points = x.Points,
                    DateRegistered = x.DateRegistered,
                    IsDeleted = x.IsDeleted,
                    IsActivated = x.IsActivated
                });
            }
        }
        public IQueryable<Models.User> AdminRepo
        {
            get
            {
                return _context.Admins.Select(x => new Models.User
                {
                    Id = x.ID,
                    Name = x.Name,
                    Hash = x.Hash,
                    Salt = x.Salt,
                });
            }
        }
        public IQueryable<Models.RegisterURL> UserRegisterRepo
        {
            get
            {
                return _context.RegisterURLs.Select(x => new Models.RegisterURL
                {
                    Id = x.ID,
                    UserId = x.UserId,
                    RegisterUrl = x.RegisterUrl1,
                    DateGenerated = x.DateGenerated
                });
            }
        }
        public IQueryable<Models.ResetPasswordURL> UserPassResetRepo
        {
            get
            {
                return _context.ResetPasswordURLs.Select(x => new Models.ResetPasswordURL
                {
                    Id = x.ID,
                    UserId = x.UserId,
                    ResetPasswordUrl = x.ResetPasswordUrl1,
                    DateGenerated = x.DateGenerated
                });
            }
        }


        /// <summary>
        /// CRUD - Services voeren hier hun CRUD statements mee uit
        /// </summary>
        public void InsertUser(Models.User user)
        {
            User userToInsert = new User
            {
                Name = user.Name,
                Hash = user.Hash,
                Salt = user.Salt,
                Email = user.Email,
                DateRegistered = user.DateRegistered,
                IsActivated = user.IsActivated,
                IsDeleted = user.IsDeleted,
                Points = user.Points
            };

            _context.Users.InsertOnSubmit(userToInsert);
            _context.SubmitChanges();
        }
        public void UpdateUser(Models.User user)
        {
            User userToUpdate = _context.Users.First(x => x.ID.Equals(user.Id));
            userToUpdate.Name = user.Name;
            userToUpdate.Hash = user.Hash;
            userToUpdate.Salt = user.Salt;
            userToUpdate.Email = user.Email;
            userToUpdate.Points = user.Points;
            userToUpdate.DateRegistered = user.DateRegistered;
            userToUpdate.IsActivated = user.IsActivated;
            userToUpdate.IsDeleted = user.IsDeleted;

            _context.SubmitChanges();
        }
        public void RemoveUser(Models.User user)
        {
            User userToRemove = _context.Users.First(x => x.ID.Equals(user.Id));
            _context.Users.DeleteOnSubmit(userToRemove);
            _context.SubmitChanges();
        }

        public void InsertRegisterUrl(Models.RegisterURL registerUrl)
        {
            RegisterURL registerUrlToInsert = new RegisterURL
            {
                UserId = registerUrl.UserId,
                RegisterUrl1 = registerUrl.RegisterUrl,
                DateGenerated = registerUrl.DateGenerated
            };

            _context.RegisterURLs.InsertOnSubmit(registerUrlToInsert);
            _context.SubmitChanges();
        }
        public void UpdateRegisterUrl(Models.RegisterURL registerUrl)
        {
            RegisterURL registerUrlToUpdate = _context.RegisterURLs.First(x => x.ID.Equals(registerUrl.Id));
            registerUrlToUpdate.UserId = registerUrl.UserId;
            registerUrlToUpdate.RegisterUrl1 = registerUrl.RegisterUrl;
            registerUrlToUpdate.DateGenerated = registerUrl.DateGenerated;
            
            _context.SubmitChanges();
        }
        public void RemoveRegisterUrl(Models.RegisterURL registerUrl)
        {
            RegisterURL registerUrlToRemove = _context.RegisterURLs.First(x => x.ID.Equals(registerUrl.Id));
            _context.RegisterURLs.DeleteOnSubmit(registerUrlToRemove);
            _context.SubmitChanges();
        }

        public void InsertResetPasswordUrl(Models.ResetPasswordURL resetPasswordUrl)
        {
            ResetPasswordURL resetPasswordUrlToInsert = new ResetPasswordURL
            {
                UserId = resetPasswordUrl.UserId,
                ResetPasswordUrl1 = resetPasswordUrl.ResetPasswordUrl,
                DateGenerated = resetPasswordUrl.DateGenerated
            };

            _context.ResetPasswordURLs.InsertOnSubmit(resetPasswordUrlToInsert);
            _context.SubmitChanges();
        }
        public void UpdateResetPasswordUrl(Models.ResetPasswordURL resetPasswordUrl)
        {
            ResetPasswordURL resetPasswordUrlToUpdate = _context.ResetPasswordURLs.First(x => x.ID.Equals(resetPasswordUrl.Id));
            resetPasswordUrlToUpdate.UserId = resetPasswordUrl.UserId;
            resetPasswordUrlToUpdate.ResetPasswordUrl1 = resetPasswordUrl.ResetPasswordUrl;
            resetPasswordUrlToUpdate.DateGenerated = resetPasswordUrl.DateGenerated;
            
            _context.SubmitChanges();
        }
        public void RemoveResetPasswordUrl(Models.ResetPasswordURL resetPasswordUrl)
        {
            ResetPasswordURL resetPasswordUrlToRemove = _context.ResetPasswordURLs.First(x => x.ID.Equals(resetPasswordUrl.Id));

            _context.ResetPasswordURLs.DeleteOnSubmit(resetPasswordUrlToRemove);
            _context.SubmitChanges();
        }
    }
}
